import Api from '@/api'
import ls from '@/lib/ls'
import dayjs from 'dayjs'
import useConfigStore from '@/store/config'

// 配置
const postForm = ref({
    reserveTime:dayjs().format('YYYY-MM-DD HH:mm:ss'),
    roomState:'',
    typeCode:'',
    pageSize: 1000,
    pageIndex: 1
  })
const maps = {
    // 门店信息
    shopInfo:{
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetBaseConfigInfo","contentData": {}}),
        set: function(resp) {
            this.shopInfo = resp
        }
    },
    // 房台信息
    roomList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetReserveRoomList","contentData":postForm.value}),
        set: function(resp) {
            this.roomList = resp.result
        }
    },
    // 客户来源
     sourceList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetDicData","contentData": {"parentCode":"104"}}),
        set: function(resp) {
            this.sourceList = resp.result
        }
    },
    // 特殊要求列表
    specialList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetDicData","contentData": {"parentCode":"105"}}),
        set: function(resp) {
            this.specialList = resp.result
        }
    },
    // 取消原因
    cancelList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetDicData","contentData": {"parentCode":"106"}}),
        set: function(resp) {
            this.cancelList = resp.result
        }
    },
    // 服务员列表
    waiterList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName":"getWaiterList","contentData":""}),
        set: function(resp) {
            this.waiterList = resp.result
        }
    },
    // 门店分区
    roomArea:{
        get: () => Api.Common.PcObjProcess.Do({"functionName":"getRoomArea","contentData":""}),
        set: function(resp) {
            this.roomArea = resp.result
        }
    },
    // 订台人列表
    customerManageList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName":"getCustomerManageList","contentData":""}),
        set: function(resp) {
            this.customerManageList = resp.result
        }
    },
    // 房台类型
    zoneList: {
        get: () => Api.Common.PcObjProcess.Do({"functionName": "GetDicData","contentData": {"parentCode":"101"}}),
        set: function(resp) {
            this.zoneList = resp.result
        }
    },

}


export default {
    // // 修改值
    // setState(key,val){
    //     this[key] = val
    // },
    // 
    // // 更新所有配置
    // async UpdateAll(){
    //     const awaits = Object.values(maps)
    //     const resps = await Promise.all(awaits.map(c => c.get()))
    //     awaits.forEach((item,index)=>{
    //         item.set.bind(this)(resps[index])
    //     })
    // },
    connect(){
         // 连接成功后，将 isConnected 状态设置为 true
        const configStore = useConfigStore()
        configStore.isConnected = true
    },
    disconnect() {
        const configStore = useConfigStore()
        // 断开连接或退出登录时，将 isConnected 状态设置为 false
        configStore.isConnected = false
    },
    async UpdateAll() {
        const awaits = Object.values(maps)
        const promises = awaits.map(c => c.get())
        const results = await Promise.allSettled(promises)
        results.forEach((result, index) => {
            if (result.status === 'fulfilled') {
                const resp = result.value
                awaits[index].set.bind(this)(resp)
            } else {
                console.log('接口报错:', result.reason)
                // 可以选择继续执行其他接口请求，或者忽略错误并继续执行下面的代码
            }
        })
    },
    // 更新配置
    async Update(...args) {
        const awaits = args.map((configName) => maps[configName])
        const resps = await Promise.all(awaits.map(c => c.get()))
        awaits.forEach((item, index) => {
            item.set.bind(this)(resps[index])
        })
    }
}